package com.oa.modules.approvalsec1.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.oa.modules.approvalsec1.entity.ProcessItemEntity;
import com.oa.modules.approvalsec1.entity.ProjectEntity;
import com.oa.modules.approvalsec1.entity.ProjectFileEntity;
import com.oa.modules.approvalsec1.entity.ProjectScheduleEntity;
import com.oa.modules.approvalsec1.service.ProcessItemService;
import com.oa.modules.approvalsec1.service.ProjectFileService;
import com.oa.modules.approvalsec1.service.ProjectScheduleService;
import com.oa.modules.approvalsec1.service.ProjectService;
import com.oa.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;

import static org.bytedeco.javacv.FrameRecorder.list;


/**
 * project表
 *
 * @author jiangwei
 * @email 785297226@qq.com
 * @date 2021-12-14 09:49:22
 */
@RestController
@RequestMapping("projectfile")
@Api(tags = "项目证照相关接口")
public class ProjectFileController extends AbstractController {
    @Autowired
    private ProjectFileService projectFileService;
    @Autowired
    private ProjectScheduleService projectScheduleService;
    @Autowired
    private ProcessItemService processItemService;
    @Autowired
    private ProjectController projectController;
    @Autowired
    private ProjectService projectService;


    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    @RequiresPermissions("generator:projectfile:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = projectFileService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation(value = "信息")
    public R info(String id, String projectId) {
        LambdaQueryWrapper<ProjectFileEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProjectFileEntity::getItemName, id)
                .eq(ProjectFileEntity::getProjectId, projectId);
        ProjectFileEntity projectFile = projectFileService.getOne(lambdaQueryWrapper);
        return R.ok().put("projectFile", projectFile);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody ProjectFileEntity projectFile) {
        projectFile.setCreateOper("1");
        //如果有文件，则完成
        if (null != projectFile.getUrl() && !"".equals(projectFile.getUrl())) projectFile.setStatusNow(1).setReason(null);
        if(projectFile.getReason()!=null){
            if (projectFile.getReason() == 1 || projectFile.getReason() == 2) {
                projectFile.setUrl("");
            }
        }
        projectFileService.save(projectFile);
        return R.ok();
    }

    /**
     * 修改微信端使用
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
//    @RequiresPermissions("generator:projectfile:update")
    public R update(@RequestBody ProjectFileEntity projectFile) {
        projectFile.setUpdateDate(new Date());
        projectFile.setUpdateOper("1");
        String url = projectFile.getUrl();
        int state = 0;
        if (projectFile.getUrl() != null && !"".equals(projectFile.getUrl())) {
            state = 1;
            projectFile.setReasonThat(null);
            projectFile.setReason(null);
        }
        if (projectFile.getUrl() == null||"".equals(projectFile.getUrl())) {
            projectFile.setUrl("");
        }
        UpdateWrapper<ProjectFileEntity> wrapper = new UpdateWrapper<ProjectFileEntity>().eq("id", projectFile.getId()).set("url", projectFile.getUrl()).set("reason", projectFile.getReason()).set("reason_that", projectFile.getReasonThat()).set("update_date", new Date()).set("status_now", state).set("profile",projectFile.getProfile());
        projectFileService.update(wrapper);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    @RequiresPermissions("generator:projectfile:delete")
    public R delete(@RequestBody Integer[] ids) {
        projectFileService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * rpa保存接口
     *
     * @param projectFileEntity
     * @return
     */
    @PostMapping("saveFile")
    @ApiOperation(value = "保存")
    public R saveFile(@RequestBody ProjectFileEntity projectFileEntity) {
        //拿到节点名称
        String itemName = projectFileEntity.getItemName();
        //根据节点名称  获取节点id
        LambdaQueryWrapper<ProjectScheduleEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProjectScheduleEntity::getName, itemName);
        ProjectScheduleEntity scheduleEntity = projectScheduleService.getOne(lambdaQueryWrapper);
        //根据流程id 进度id  获取 流程节点id
        LambdaQueryWrapper<ProcessItemEntity> itemLambdaQueryWrapper = new LambdaQueryWrapper<>();
        itemLambdaQueryWrapper.eq(ProcessItemEntity::getScheduleId, scheduleEntity.getId())
                .eq(ProcessItemEntity::getProcessId, projectFileEntity.getProcessId());
        //获取到流程节点id
        ProcessItemEntity processItemEntity = processItemService.getOne(itemLambdaQueryWrapper);
        //去文件表根据 项目赋码 节点id查询文件  存在修改  不存在保存
        LambdaQueryWrapper<ProjectFileEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProjectFileEntity::getProjectId, projectFileEntity.getProjectId())
                .eq(ProjectFileEntity::getItemId, processItemEntity.getId());
        Map<String, Object> map = projectFileService.getMap(wrapper);

        if (projectFileEntity.getUrl() != null && !"".equals(projectFileEntity.getUrl())) {
            projectFileEntity.setStatusNow(1);
        } else {
            projectFileEntity.setStatusNow(0);
        }
        if (map != null) {
            String url = (String) map.get("url");
            projectFileEntity.setId(Integer.parseInt(map.get("id").toString()));
            projectFileEntity.setUrl(projectFileEntity.getUrl());
            LambdaUpdateWrapper<ProjectFileEntity> updateWrapper = new LambdaUpdateWrapper<ProjectFileEntity>().eq(ProjectFileEntity::getId, projectFileEntity.getId()).set(ProjectFileEntity::getUrl, projectFileEntity.getUrl()).set(ProjectFileEntity::getStatusNow, projectFileEntity
                    .getStatusNow()).set(ProjectFileEntity::getUploadTime, projectFileEntity.getUploadTime()).set(ProjectFileEntity::getCompletionTime, projectFileEntity.getCompletionTime());
            projectFileService.update(updateWrapper);
//            projectFileService.updateById(projectFileEntity);
            return R.ok();
        }
        //  获取item的id  流程id
        ProjectFileEntity newProjectFileEntity = new ProjectFileEntity();
        newProjectFileEntity.setCreateDate(new Date());
        newProjectFileEntity.setCreateOper("1");
        newProjectFileEntity.setStatusNow(projectFileEntity.getStatusNow());
        newProjectFileEntity.setProjectId(projectFileEntity.getProjectId());
        newProjectFileEntity.setUrl(projectFileEntity.getUrl());
        newProjectFileEntity.setItemId(processItemEntity.getId());
        newProjectFileEntity.setUploadTime(projectFileEntity.getUploadTime());
        newProjectFileEntity.setCompletionTime(projectFileEntity.getCompletionTime());
        newProjectFileEntity.setItemName(projectFileEntity.getItemName());
        if (projectFileEntity.getUrl() != null && !"".equals(projectFileEntity.getUrl())) {
            newProjectFileEntity.setStatusNow(1);
        } else {
            newProjectFileEntity.setStatusNow(0);
        }
        projectFileService.save(newProjectFileEntity);
        return R.ok().put("projectFile", newProjectFileEntity);
    }

    /**
     * 修改证照节点id
     */
    @GetMapping("/updateByProcessId")
    @ApiOperation(value = "修改")
    public R updateByProcess() {
        List<ProjectEntity> list = projectService.list();
        for (ProjectEntity projectEntity : list) {
            Integer newProcessId = null;
            ProjectEntity project = projectService.getById(projectEntity.getId());
            Integer processId = project.getProcessId();
            if (project.getType() == 1) {
                //学校类项目
                newProcessId = 4;
            } else if (project.getType() == 2) {
                //医院类项目
                newProcessId = 5;
            } else if (project.getType() == 3) {
                //房地产类项目
                newProcessId = 3;
            } else if (project.getType() == 4) {
                //工业类项目
                newProcessId = 2;
            }
            //修改项目流程
            projectFileService.updateByProcess(projectEntity.getId().toString(), processId, newProcessId);
            LambdaUpdateWrapper<ProjectEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(ProjectEntity::getProcessId, newProcessId).eq(ProjectEntity::getId, projectEntity.getId());
            projectService.update(updateWrapper);
        }

        return R.ok();
    }

    @GetMapping("/tj")
    public R tj() {
        List tj = projectFileService.tj();
        return R.ok().put("tj", tj);
    }

    @GetMapping("/tjPorject")
    public R tjProject(String name) {
        LinkedHashSet result=projectFileService.tjProject(name);
        return R.ok().put("data", result);
    }

    @GetMapping("/bbldProject")
    public R bbldProject(@RequestParam Map<String, Object> params) {
        List<ProjectEntity> list = projectFileService.bbldProject(params);
        return R.ok().put("data",list);
    }

//    @GetMapping("/test")
//    public R test(){
//        List<ProjectFileEntity> list = projectFileService.list();
//        list.forEach(item->{
//            if(item.getProjectId()!=null){
//                ProjectEntity project = projectService.getOne(new QueryWrapper<ProjectEntity>().eq("coding", item.getProjectId()));
//                if(project!=null){
//                    item.setProjectId(String.valueOf(project.getId()));
//                }
//            }
//        });
//        projectFileService.updateBatchById(list);
//        return R.ok();
//    }

}
